#include "camrecevier.h"

#include "QDebug"
#include "QFileDialog"

int height = 120;
int width = 160;
QGraphicsScene *scene;
QImage *image;
QImage *filter;
uint8_t LUT5[] = {0x00, 0x08, 0x10, 0x19, 0x21, 0x29, 0x31, 0x3a,
                  0x42, 0x4a, 0x52, 0x5a, 0x63, 0x6b, 0x73, 0x7b,
                  0x84, 0x8c, 0x94, 0x9c, 0xa5, 0xad, 0xb5, 0xbd,
                  0xc5, 0xce, 0xd6, 0xde, 0xe6, 0xef, 0xf7, 0xff,
              };

uint8_t LUT6[] = {0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
                  0x20, 0x24, 0x28, 0x2d, 0x31, 0x35, 0x39, 0x3d,
                  0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d,
                  0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d,
                  0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
                  0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
                  0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd7, 0xdb, 0xdf,
                  0xe3, 0xe7, 0xeb, 0xef, 0xf3, 0xf7, 0xfb, 0xff,

              };

camRecevier::camRecevier(Ui::MainWindow* ui, QObject *parent) :
        QObject(parent)
{

    this->ui = ui;
    image = new QImage(width, height, QImage::Format_RGB888);
    filter = new QImage(width, height, QImage::Format_RGB888);
    //    delete scene;
    scene = new QGraphicsScene();
    QTransform transform;
    int RGB;
    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            RGB = 255 << 16 | 255 << 8 | 255;

            image->setPixel(j,i,RGB);
        }

    }

    ui->graphicsView_cam->setScene(scene);
    ui->graphicsView_cam->scene()->addPixmap(
            (QPixmap::fromImage(*image).scaled(320,240,
                                               Qt::KeepAspectRatio,Qt::FastTransformation)).transformed(transform));

    connect(ui->pushButton_savePic,SIGNAL(clicked()),this,SLOT(save()));

}

void camRecevier::save()
{
    QString filename;
    filename = QFileDialog::getSaveFileName(0,QString(),QString(),"24-Bit-Bitmap (*.bmp);;JPG (*.jpg);;PNG (*.png)");

    if (filename.length()==0){
        return;
    }

    image->scaled(width,height).save(filename,0);
    filter->scaled(width,height).save(filename+"Filter.bmp",0);
}

void camRecevier::receiveImage(QByteArray data) {

    delete scene;
    scene = new QGraphicsScene();
    QTransform transform;
    int RGB;
    uint8_t b1, b2, R5 = 0, G6 = 0, B5 = 0;
    int count = 0;
    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            b1 = data[i * width * 2 + j * 2];
            b2 = data[i * width * 2 + j * 2 + 1];
            R5 = (uint8_t) ((b1 & 0xF8) >> 3);
            G6 = (uint8_t) (( ((b1 & 0x07) << 3) | ((b2 & 0xE0) >> 5)));
            B5 = (uint8_t) (b2 & 0x1F);
            // my First Filter
//            if(LUT5[R5] > 120 && LUT5[B5] < 110 && LUT6[G6] < 110 && (LUT5[R5] + LUT6[G6] + LUT5[B5] > 200) && (LUT5[R5] + LUT6[G6] + LUT5[B5] < 400)){
//                    RGB = (255 << 16) | (0 << 8) | 0;
//            } else {
//                RGB = 0;
//            }

            // Photoshop idea filer

            if(255-LUT5[R5] > 128){
               RGB = (0 << 16) | (0 << 8) | 0;
            } else {
                if((LUT6[G6]) > 115){
                    RGB = (0 << 16) | (0 << 8) | 0;
                } else {
                    if((LUT5[B5]) > 95){
                        RGB = (0 << 16) | (0 << 8) | 0;
                    } else {
                        RGB = ((LUT5[R5]) << 16) | ((LUT6[G6]) << 8) | (LUT5[B5]);
                        count++;
                    }
                }
            }
            filter->setPixel(j,i,RGB);

            RGB = (LUT5[R5] << 16) | (LUT6[G6] << 8) | LUT5[B5];
//            qDebug() << RGB;
            image->setPixel(j,i,RGB);
        }

    }
    qDebug() << count;
    int w = ui->graphicsView_cam->width();
//    int h = ui->graphicsView_cam->height();

//    qDebug() << "widht" << w << "height" << h;
//    qDebug() << (w/width);
//    qDebug() << "Scale to " << (w/width) * width << (w/width) * height;


    ui->graphicsView_cam->setScene(scene);
    if(ui->checkBox_filter->isChecked()){
        ui->graphicsView_cam->scene()->addPixmap((QPixmap::fromImage(*filter).scaled((w/width) * width - 4,(w/width) * height - 4,Qt::KeepAspectRatio,Qt::FastTransformation)).transformed(transform));
    } else {
        ui->graphicsView_cam->scene()->addPixmap((QPixmap::fromImage(*image).scaled((w/width) * width - 4,(w/width) * height - 4,Qt::KeepAspectRatio,Qt::FastTransformation)).transformed(transform));
    }
}
